package com.ruoyi.yljf.controller.AdminController;

import java.util.List;
import java.util.Arrays;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.yljf.domain.BaseQuestionBank;
import com.ruoyi.yljf.service.IBaseQuestionBankService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.yljf.domain.ExerciseQuestion;
import com.ruoyi.yljf.service.IExerciseQuestionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 练习题Controller
 * 
 * @author metaee
 * @date 2025-04-25
 */
@RestController
@RequestMapping("/yljf/ExerciseQuestion")
public class ExerciseQuestionController extends BaseController {
    @Autowired
    private IExerciseQuestionService exerciseQuestionService;

    @Resource
    private IBaseQuestionBankService baseQuestionBankService;

    /**
     * 查询练习题列表
     */
    @PreAuthorize("@ss.hasPermi('yljf:ExerciseQuestion:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExerciseQuestion exerciseQuestion) {
        startPage();
        List<ExerciseQuestion> list = exerciseQuestionService.selectExerciseQuestionList(exerciseQuestion);
        list.forEach(exerciseQuestion1 -> {
            BaseQuestionBank baseQuestionBank = baseQuestionBankService.selectByid(exerciseQuestion1.getQuestionId());
            exerciseQuestion1.setQuestionOptions(baseQuestionBank.getQuestionOptions());
            exerciseQuestion1.setQuestionTitle(baseQuestionBank.getQuestionTitle());
        });
        return getDataTable(list);
    }

    /**
     * 导出练习题列表
     */
    @PreAuthorize("@ss.hasPermi('yljf:ExerciseQuestion:export')")
    @Log(title = "练习题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExerciseQuestion exerciseQuestion) {
        List<ExerciseQuestion> list = exerciseQuestionService.selectExerciseQuestionList(exerciseQuestion);
        ExcelUtil<ExerciseQuestion> util = new ExcelUtil<ExerciseQuestion>(ExerciseQuestion.class);
        util.exportExcel(response, list, "练习题数据");
    }

    /**
     * 获取练习题详细信息
     */
    @PreAuthorize("@ss.hasPermi('yljf:ExerciseQuestion:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(exerciseQuestionService.getById(id));
    }

    /**
     * 新增练习题
     */
    @PreAuthorize("@ss.hasPermi('yljf:ExerciseQuestion:add')")
    @Log(title = "练习题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExerciseQuestion exerciseQuestion) {
        return toAjax(exerciseQuestionService.save(exerciseQuestion));
    }

    /**
     * 修改练习题
     */
    @PreAuthorize("@ss.hasPermi('yljf:ExerciseQuestion:edit')")
    @Log(title = "练习题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExerciseQuestion exerciseQuestion) {
        return toAjax(exerciseQuestionService.updateById(exerciseQuestion));
    }

    /**
     * 删除练习题
     */
    @PreAuthorize("@ss.hasPermi('yljf:ExerciseQuestion:remove')")
    @Log(title = "练习题", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(exerciseQuestionService.removeByIds(Arrays.asList(ids)));
    }
}
